package user_api

import (
	"gin_study_blog/common/blog_type"
	"gin_study_blog/common/http"
	"gin_study_blog/models"
	"gin_study_blog/service/helper"
	"gin_study_blog/utils"
	"github.com/gin-gonic/gin"
)

// UserListView 用户列表
func (UserApi) UserListView(c *gin.Context) {
	userInfo, _ := c.Get("user")
	claims := userInfo.(*utils.UserClaims)

	var page models.PageInfo
	if err := c.ShouldBindQuery(&page); err != nil {
		http.FailWithCode(http.ParamsError, c)
		return
	}

	userList := make([]*models.UserModel, 0)
	list, count, err := helper.CommonList(&models.UserModel{}, helper.Option{
		PageInfo: page,
	})
	if err != nil {
		http.FailWithCode(http.SettingsError, c)
		return
	}
	for _, user := range list {

		if blog_type.Role(claims.Role) == blog_type.PermissionVisitor {
			// 游客
			user.UserName = ""
		}
		if blog_type.Role(claims.Role) != blog_type.PermissionAdmin {
			// 脱敏
			user.Tel = helper.DesensitizationTel(user.Tel)
			user.Email = helper.DesensitizationEmail(user.Email)
		}
		userList = append(userList, user)
	}

	http.IsOkWithList(list, count, c)
}
